#__Author:Administrator
#__Date:2019/7/25

from config import setting
import socket
import os

def login(conn):
    while True:
        user = input("login>>").strip()
        pwd = input("password>>").strip()
        user_info = "%s %s" % (user, pwd)
        conn.sendall(bytes(user_info, 'utf-8'))
        status = conn.recv(1024)
        status = str(status, "utf-8")
        if status == "4002":
            print("登录成功")
            break
        elif status == "4003":
            print("登录失败,用户名或密码错误")

def useradd(conn, inpt):
    conn.sendall(bytes(inpt, 'utf-8'))
    while True:
        status = str(conn.recv(1024), "utf-8")
        if status == "4006":
            pwd = input("密码>>").strip()
            conn.sendall(bytes(pwd, 'utf-8'))
            continue
        elif status == "4004":
            print("\033[34m 注册成功 \033[0m")
            break
        else:
            print("\033[34m 注册失败 %s \033[0m" % status)
            break

def cmd(conn, inpt):
    conn.sendall(bytes(inpt, "utf-8"))
    basic_data = str(conn.recv(1024), "utf-8")
    if basic_data == "4001":
        login(conn)
    else:
        data_info = basic_data.split(" ")
        data_size = int(data_info[1])
        has_size = 0
        conn.sendall(bytes("ack", "utf-8"))
        while has_size != data_size:
            data = conn.recv(1024)
            has_size += len(data)
            print(str(data, "gbk"))



def post(conn, inpt):
    src = inpt.split(" ")[1]
    #判断是否指定目标目录，默认上传到home根目录
    try:
        dest = inpt.split(" ")[2]
    except IndexError:
        inpt = inpt + " /"

    data_size = os.path.getsize(src)
    data_info = "%s %s" % (inpt, data_size)
    conn.sendall(bytes(data_info, "utf-8"))
    ack = str(conn.recv(1024), "utf-8")
    has_send = 0
    f = open(src, 'rb')
    if ack == "4001":
        login(conn)
    else:
        if ack == "2003":
            choice = input("文件已经存在，是否续传【y/n】?>>").strip()
            if choice.lower() == 'y':
                conn.sendall(bytes("2004", "utf-8"))
                has_send = int(str(conn.recv(1024), "utf-8"))
                #更改文件读取指针位置
                f.seek(has_send)
            else:
                conn.sendall(bytes("2005", "utf-8"))

        while has_send < data_size:
            if ack == "3000":
                print("目录不存在")
                break
            data = f.read(1024)
            conn.sendall(data)
            has_send += len(data)
        f.close()
        print("上传成功")

def get(conn, inpt):
    pass

def help_info():
    print("""
    useradd username
    cmd command
    post files path
    get download files path
    exit exit program
    """)

def execute(conn):
    choice = {
        "useradd": useradd,
        "help": help_info,
        "cmd": cmd,
        "post": post,
        "get": get
    }
    while True:
        inpt = input(">>").strip()
        command = inpt.split(" ")
        if command[0] in choice:
            func = choice[command[0]]
            func(conn, inpt)
        else:
            print("输入有误")
            help_info()

def main():
    conn = socket.socket()
    conn.connect((setting.SERVER_HOST, setting.SERVER_PORT))
    welcome_bytes = conn.recv(1024)
    print(str(welcome_bytes, "utf-8"))
    execute(conn)
    conn.close()
